package com.bins.springboot.dubbo.api.thread;
 
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
 
import java.io.FileInputStream;
 
import java.io.FileOutputStream;
 
import java.nio.MappedByteBuffer;
 
import java.nio.channels.FileChannel;
 
public class FileCopyTest
{

    public static void main(String[] args) {
        File[] files = new File("D:\\Pictures").listFiles();
        String destFolderPath = "E:\\Pictures";
 
        long t = System.currentTimeMillis();
 
        try
        {
            for (File sourceFile : files)
            {
                traditionalCopy(sourceFile.getPath(), destFolderPath + "/" + sourceFile.getName());
            }
 
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        System.out.println("传统IO方法实现文件拷贝耗时:" + (System.currentTimeMillis() - t) + "ms");
 
        //删除刚刚创建的
        for (File file : new File(destFolderPath).listFiles())
        {
            file.delete();
        }
 
        t = System.currentTimeMillis();
        try
        {
            for (File sourceFile : files)
            {
                copyBIO(sourceFile.getPath(), destFolderPath + "/" + sourceFile.getName());
            }
 
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        System.out.println("传统IO + 缓存 方法实现文件拷贝耗时:" + (System.currentTimeMillis() - t) + "ms");
 
        //删除刚刚创建的
        for (File file : new File(destFolderPath).listFiles())
        {
            file.delete();
        }
 
        t = System.currentTimeMillis();
        try
        {
            for (File sourceFile : files)
            {
                nioCopy(sourceFile.getPath(), destFolderPath + "/" + sourceFile.getName());
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        System.out.println("利用NIO文件通道方法实现文件拷贝耗时:" + (System.currentTimeMillis() - t) + "ms");
 
        //删除刚刚创建的
        for (File file : new File(destFolderPath).listFiles())
        {
            file.delete();
        }
 
        t = System.currentTimeMillis();
        try
        {
            for (File sourceFile : files)
            {
                nioCopy2(sourceFile.getPath(), destFolderPath + "/" + sourceFile.getName());
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        System.out.println("利用NIO文件内存映射及文件通道实现文件拷贝耗时:" + (System.currentTimeMillis() - t) + "ms");
 
//        //删除刚刚创建的
//        for (File file : new File(destFolderPath).listFiles())
//        {
//            file.delete();
//        }
 
    }
 
    private static void traditionalCopy(String sourcePath, String destPath) throws Exception
    {
        File source = new File(sourcePath);
        File dest = new File(destPath);
 
        FileInputStream fis = new FileInputStream(source);
        FileOutputStream fos = new FileOutputStream(dest);
         
        byte[] buf = new byte[8192];
        int len = 0;
 
        while ((len = fis.read(buf)) != -1)
        {
            fos.write(buf, 0, len);
        }
 
        fis.close();
        fos.close();
    }
 
    private static void copyBIO(String sourcePath, String destPath) throws Exception
    {
        File source = new File(sourcePath);
        File dest = new File(destPath);
 
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(source));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dest));
 
        byte[] buf = new byte[8192];
        int len = 0;
        while ((len = bis.read(buf)) != -1)
        {
            bos.write(buf, 0, len);
        }
 
        bis.close();
        bos.close();
    }
 
    private static void nioCopy(String sourcePath, String destPath) throws Exception
    {
        File source = new File(sourcePath);
        File dest = new File(destPath);
 
        FileInputStream fis = new FileInputStream(source);
        FileOutputStream fos = new FileOutputStream(dest);
 
        FileChannel sourceCh = fis.getChannel();
        FileChannel destCh = fos.getChannel();


        destCh.transferFrom(sourceCh, 0, sourceCh.size());
 
        sourceCh.close();
        destCh.close();
 
    }
 
    private static void nioCopy2(String sourcePath, String destPath) throws Exception
    {
        File source = new File(sourcePath);
        File dest = new File(destPath);
 
        FileInputStream fis = new FileInputStream(source);
        FileOutputStream fos = new FileOutputStream(dest);
 
        FileChannel sourceCh = fis.getChannel();
        FileChannel destCh = fos.getChannel();
 
        MappedByteBuffer mbb = sourceCh.map(FileChannel.MapMode.READ_ONLY, 0, sourceCh.size());
 
        destCh.write(mbb);
 
        sourceCh.close();
        destCh.close();
    }
 
}
